﻿using Groep2TINGHService.Get_Set_Classes;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Groep2TINGHService.DB_Classes
{
    public class ContractDB
    {
        //Geeft een lijst terug met alle Contracts
        public static List<Contract> GetAllContracts()
        {
            List<Contract> lijst = new List<Contract>();
            SqlConnection connection = new SqlConnection(ConnectionDB.GetConnection());
            SqlCommand selectcommand = new SqlCommand("select * from Contract", connection);
            try
            {
                Contract temp;
                connection.Open();
                SqlDataReader reader = selectcommand.ExecuteReader();
                int Id = reader.GetOrdinal("Id");
                int Number = reader.GetOrdinal("Number");
                int Startdate = reader.GetOrdinal("Startdate");
                int EndDate = reader.GetOrdinal("EndDate");
                int CompanyId = reader.GetOrdinal("CompanyId");
                int ContractFormulaId = reader.GetOrdinal("ContractFormulaId");
                int ContractRunning = reader.GetOrdinal("ContractRunning");

                while (reader.Read())
                {
                    temp = new Contract();
                    temp.Id = reader.GetGuid(Id);
                    temp.Number = reader.GetString(Number);
                    temp.StartDate = reader.GetDateTime(Startdate);
                    temp.EndDate = reader.GetDateTime(EndDate);
                    temp.CompanyId = reader.GetGuid(CompanyId);
                    temp.ContractFormulaId = reader.GetGuid(ContractFormulaId);
                    temp.ContractRunning = reader.GetBoolean(ContractRunning);
                    lijst.Add(temp);

                }
                reader.Close();
                return lijst;
            }
            catch (SqlException ex)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
        }

        //Wijzigen van Contract Status
        public static bool ModifyContractRunning()
        {
            SqlConnection connection = new SqlConnection(ConnectionDB.GetConnection());
            string modify = "UPDATE Company " +
                            "SET Street=@Street, Zipcode=@Zipcode, City=@City, Country=@Country, Email=@Email, Phone=@Phone, Employees=@Employees " +
                            "WHERE Name = @Name";
            SqlCommand modifyCommand = new SqlCommand(modify, connection);
            modifyCommand.Parameters.AddWithValue("@Name", name);
            modifyCommand.Parameters.AddWithValue("@Street", company.Street);
            modifyCommand.Parameters.AddWithValue("@Zipcode", company.ZipCode);
            modifyCommand.Parameters.AddWithValue("@City", company.City);
            modifyCommand.Parameters.AddWithValue("@Country", company.Country);
            modifyCommand.Parameters.AddWithValue("@Email", company.Email);
            modifyCommand.Parameters.AddWithValue("@Phone", company.Phone);
            modifyCommand.Parameters.AddWithValue("@Employees", company.Employees);


            try
            {
                connection.Open();
                int count = modifyCommand.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (SqlException ex)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
        }

    }
}